<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <!-- <el-form-item label="部门" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="部门" prop="deptId">
        <treeselect style="width: 240px" clearable v-model="queryParams.deptId" :options="deptOptions" :show-count="true"
          placeholder="请选择归属部门" />
      </el-form-item>
      <el-form-item label="行政班级编号" prop="metaclassNo">
        <el-input v-model="queryParams.metaclassNo" placeholder="请输入行政班级编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="行政班级名称" prop="metaclassName">
        <el-input v-model="queryParams.metaclassName" placeholder="请输入行政班级名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-input v-model="queryParams.grade" placeholder="请输入年级" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['score:metaclass:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['score:metaclass:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['score:metaclass:remove']">删除</el-button>
      </el-col>
	  <el-col :span="1.5">
	    <el-button
	      type="warning"
	      plain
	      icon="el-icon-upload2"
	      size="mini"
	      @click="importScore"
	    >批量导入
	    </el-button>
	  </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['score:metaclass:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="metaclassList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="行政班级id" align="center" prop="meataclassId" />
      <el-table-column label="行政班级编号" align="center" prop="metaclassNo" />
      <!-- <el-table-column label="行政班级名称" align="center" prop="metaclassName" /> -->
      <el-table-column label="行政班级名称" align="center" prop="metaclassName">
  <template slot-scope="scope">
    <el-link type="primary" @click="handleClassClick(scope.row)">{{ scope.row.metaclassName }}</el-link>
  </template>
</el-table-column>
      <el-table-column label="年级" align="center" prop="grade" />
      <!-- <el-table-column label="部门" align="center" prop="deptId" /> -->
      <el-table-column label="部门" align="center" prop="dept.deptName" />
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['score:metaclass:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['score:metaclass:remove']">删除</el-button>
		 <!-- <el-button size="mini" type="info" icon="el-icon-upload2" @click="handleupload(scope.row)"
		    v-hasPermi="['score:metaclass:import']">导入</el-button>	 -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改班级管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="行政班级编号" prop="metaclassNo">
          <el-input v-model="form.metaclassNo" placeholder="请输入行政班级编号" />
        </el-form-item>
        <el-form-item label="行政班级名称" prop="metaclassName">
          <el-input v-model="form.metaclassName" placeholder="请输入行政班级名称" />
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入年级" />
        </el-form-item>
        <!-- <el-form-item label="部门" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门" />
        </el-form-item> -->
        <!-- <el-col :span="12"> -->
        <el-form-item label="归属部门" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
        </el-form-item>



        <!-- <el-form-item label="岗位">
          <el-select v-model="form.postIds" multiple placeholder="请选择岗位">
            <el-option v-for="item in postOptions" :key="item.postId" :label="item.postName" :value="item.postId"
              :disabled="item.status == 1"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="岗位">
          <el-select v-model="form.postIds" multiple placeholder="请选择岗位">
            <el-option v-for="item in postOptions" :key="item.postId" :label="item.postName" :value="item.postId"
              :disabled="item.status == 1"></el-option>
          </el-select>
        </el-form-item> -->


        <!-- <el-form-item label="上课班级" prop="courseclassId">
          <el-select v-model="form.courseclassId" multiple filterable placeholder="请选择上课班级">
            <el-option v-for="item in courseclassList" :key="item.courseclassId" :label="item.courseclassNo"
              :value="item.courseclassId" :disabled="item.status == 1"></el-option>
          </el-select>
        </el-form-item> -->


        <el-divider content-position="center">行政班级上课信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini"
              @click="handleAddCourseclassMetaclass">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini"
              @click="handleDeleteCourseclassMetaclass">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="courseclassMetaclassList" :row-class-name="rowCourseclassMetaclassIndex"
          @selection-change="handleCourseclassMetaclassSelectionChange" ref="courseclassMetaclass">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />

          <el-form-item label="上课班级" prop="courseclassId">
            <!-- <el-input v-model="queryParams.courseclassId" placeholder="请输入上课班级" clearable @keyup.enter.native="handleQuery" /> -->
            <el-select v-model="form.courseclassId" filterable placeholder="请选择上课班级">
              <el-option v-for="item in courseclassList" :key="item.courseclassId" :label="item.courseclassNo"
                :value="item.courseclassId" :disabled="item.status == 1"></el-option>
            </el-select>
          </el-form-item>

          <el-table-column label="上课班级" prop="courseclassId" width="200px">
            <template slot-scope="scope">
              <!-- <el-input v-model="scope.row.courseclassId" placeholder="请选择上课班级" /> -->
              <el-select v-model="scope.row.courseclassId" filterable placeholder="请选择上课班级">
                <el-option v-for="item in courseclassList" :key="item.courseclassId" :label="item.courseclassNo"
                  :value="item.courseclassId" :disabled="item.status == 1"></el-option>
              </el-select>
            </template>
          </el-table-column>

          <el-table-column label="上课班级名" prop="course.courseName" width="160px">
            <template slot-scope="scope">
              <!-- <el-input v-model="scope.row.courseclassId" placeholder="请选择上课班级" /> -->
              <el-select v-model="scope.row.courseclassId" filterable placeholder="请选择上课班级">
                <el-option v-for="item in courseclassList" :key="item.courseclassId" :label="item.course.courseName"
                  :value="item.courseclassId" :disabled="item.status == 1"></el-option>
              </el-select>
            </template>
          </el-table-column>


        </el-table>
        <!-- </el-col> -->
        <!-- <el-form-item label="删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
	<!-- <el-dialog title="导入数据信息" visible="daoru">
		 <el-table
		    :data="tableData"
		    style="width: 100%"
		    height="250">
		    <el-table-column
		      fixed
		      prop="date"
		      label="日期"
		      width="150">
		    </el-table-column>
		    <el-table-column
		      prop="name"
		      label="姓名"
		      width="120">
		    </el-table-column>
		    <el-table-column
		      prop="province"
		      label="省份"
		      width="120">
		    </el-table-column>
		    <el-table-column
		      prop="city"
		      label="市区"
		      width="120">
		    </el-table-column>
		    <el-table-column
		      prop="address"
		      label="地址"
		      width="300">
		    </el-table-column>
		    <el-table-column
		      prop="zip"
		      label="邮编"
		      width="120">
		    </el-table-column>
		  </el-table>
	</el-dialog> -->

	<el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
	  <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
	    :action="upload.url " :disabled="upload.isUploading"
	    :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
	    <i class="el-icon-upload"></i>
	    <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
	    <div class="el-upload__tip text-center" slot="tip">
	     <!-- <div class="el-upload__tip" slot="tip">
	        <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的数据
	      </div> -->
	      <span>仅允许导入xls、xlsx格式文件。</span>
	     <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
	        @click="importTemplate">下载模板</el-link>
	    </div>
	  </el-upload>
	  <div slot="footer" class="dialog-footer">
	    <el-button type="primary" @click="submitFileForm">确 定</el-button>
	    <el-button @click="upload.open = false">取 消</el-button>
	  </div>
	  </el-dialog>

  </div>
</template>

<script>
import { listMetaclass, getMetaclass, delMetaclass, addMetaclass, updateMetaclass } from "@/api/score/metaclass";
import { deptTreeSelect } from "@/api/system/user";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getToken } from "@/utils/auth";
import { listCourseclass } from "@/api/score/courseclass";

export default {
  name: "Metaclass",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
	  upload: {
	    // 是否显示弹出层（用户导入）
	    open: false,
	    // 弹出层标题（用户导入）
	    title: "",
	    // 是否禁用上传
	    isUploading: false,
	    // 是否更新已经存在的用户数据
	    updateSupport: 0,
	    // 设置上传的请求头部
	    headers: { Authorization: "Bearer " + getToken() },
	    // 上传的地址
	    url: process.env.VUE_APP_BASE_API + "/score/metaclass/importData"
	  },
      // 班级管理表格数据
      metaclassList: [],
      // 行政班级上课表格数据
      courseclassMetaclassList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门树选项
      deptOptions: [],
      // 课程班级表格数据
      courseclassList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        metaclassNo: null,
        metaclassName: null,
        grade: null,
        deptId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询班级管理列表 */
    getList() {
      this.loading = true;
      listMetaclass(this.queryParams).then(response => {
        this.metaclassList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      /** 查询课程班级列表 */
      listCourseclass().then(response => {
        this.courseclassList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        meataclassId: null,
        metaclassNo: null,
        metaclassName: null,
        grade: null,
        deptId: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.courseclassMetaclassList = [];
      this.resetForm("form");
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
  handleClassClick(row) {
    console.log("meataclassId:",row.meataclassId)
  // 使用路由跳转到学生列表页面，传递班级ID作为参数
  this.$router.push({
    path: '/student/list',
    query: {
      metaclassId: row.meataclassId
    }
  });
},
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.meataclassId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加班级管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const meataclassId = row.meataclassId || this.ids
      getMetaclass(meataclassId).then(response => {
        this.form = response.data;
        this.courseclassMetaclassList = response.data.courseclassMetaclassList;
        this.open = true;
        this.title = "修改班级管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.courseclassMetaclassList = this.courseclassMetaclassList;
          if (this.form.meataclassId != null) {
            updateMetaclass(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMetaclass(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const meataclassIds = row.meataclassId || this.ids;
      this.$modal.confirm('是否确认删除班级管理编号为"' + meataclassIds + '"的数据项？').then(function () {
        return delMetaclass(meataclassIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 行政班级上课序号 */
    rowCourseclassMetaclassIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 行政班级上课添加按钮操作 */
    handleAddCourseclassMetaclass() {
      let obj = {};
      this.courseclassMetaclassList.push(obj);
    },
    /** 行政班级上课删除按钮操作 */
    handleDeleteCourseclassMetaclass() {
      if (this.checkedCourseclassMetaclass.length == 0) {
        this.$modal.msgError("请先选择要删除的行政班级上课数据");
      } else {
        const courseclassMetaclassList = this.courseclassMetaclassList;
        const checkedCourseclassMetaclass = this.checkedCourseclassMetaclass;
        this.courseclassMetaclassList = courseclassMetaclassList.filter(function (item) {
          return checkedCourseclassMetaclass.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleCourseclassMetaclassSelectionChange(selection) {
      this.checkedCourseclassMetaclass = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('score/metaclass/export', {
        ...this.queryParams
      }, `metaclass_${new Date().getTime()}.xlsx`)
    },
	importScore(){
		this.upload.title = "导入行政班级信息";
		this.upload.open = true;
	},
	// 文件上传中处理
	handleFileUploadProgress(event, file, fileList) {
	  this.upload.isUploading = true;
	},
	// 文件上传成功处理
	handleFileSuccess(response, file, fileList) {
	  this.upload.open = false;
	  this.upload.isUploading = false;
	  this.$refs.upload.clearFiles();
	  this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
	  this.getList();
	},
	// 提交上传文件
	submitFileForm() {
	  this.$refs.upload.submit();
	},
	importTemplate(){
		this.download('score/metaclass/export1', {
		  ...this.queryParams
		}, `metaclass_${new Date().getTime()}.xlsx`)
	}
  }
};
</script>
